package aom.ju.ss.utils;
/*
 * Created by sunning on 2019/8/7.
 */

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.DisplayMetrics;

import androidx.multidex.MultiDexApplication;

import com.danikula.videocache.HttpProxyCacheServer;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import aom.ju.ss.R;
import aom.ju.ss.utils.zxing.activity.ZXingLibrary;

public class BaseApplication extends MultiDexApplication {

    public String getVersionName() {
        try {
            return getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0.0";
    }

    public Long getAppVersion() {
        long version = 0L;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            try {
                PackageInfo packageInfo =
                        getPackageManager().getPackageInfo(this.getPackageName(), 0);
                version = packageInfo.versionCode;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                PackageInfo packageInfo =
                        getPackageManager().getPackageInfo(this.getPackageName(), 0);
                version = packageInfo.getLongVersionCode();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return version;
    }


    /**
     * 对外提供整个应用生命周期的MyApplication
     **/
    private static BaseApplication instance;

    public static BaseApplication getInstance() {
        return instance;
    }

    public int w, h;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        DisplayMetrics dm;
        dm = getResources().getDisplayMetrics();
        w = dm.widthPixels;
        h = dm.heightPixels;


        ZXingLibrary.initDisplayOpinion(this);
        initChannel();
        //SmartRefreshLayout  设置默认风格
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context).setDrawableSize(20f));

        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.white, R.color.color_333333);
            return new ClassicsHeader(context);
        });


    }

    private void initChannel() {
        String channelNumber = "-1";//未知
        if ("".equals(SpUtils.getString("CHANNEL"))) {
            try {
                ApplicationInfo pm = BaseApplication.getInstance().getPackageManager().getApplicationInfo(BaseApplication.getInstance().getPackageName(), PackageManager.GET_META_DATA);
                channelNumber = pm.metaData.getString("CHANNEL");
                SpUtils.putString("CHANNEL", channelNumber);
            } catch (Exception e) {
                LogUtils.e("获取渠道号失败");
                e.printStackTrace();
            }
        } else {
            channelNumber = SpUtils.getString("CHANNEL");
        }
        SpUtils.putString("CHANNEL", channelNumber);
    }


    private HttpProxyCacheServer proxy;
    public static HttpProxyCacheServer getProxy(Context context) {
        BaseApplication app = (BaseApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .maxCacheSize(1024 * 1024 * 1024)       // 1 Gb for cache
//                .fileNameGenerator(new MyFileNameGenerator())
                .build();
    }


}
